package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Log;
import com.victor.entity.Shopuser;
import com.victor.entity.ShopuserXQ;
import com.victor.entity.ShopuserXQphoto;
import com.victor.entity.ShopuserXQyudingsm;
import com.victor.service.LogService;
import com.victor.service.PartnerService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 服务伙伴管理
 * 
 * @author Mr.Zhang
 *
 */
@RestController
@RequestMapping("/partner")
public class PartnerController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private PartnerService service;

	@Autowired
	private LogService logservice;

	// 获取服务伙伴列表(根据类别查询 ,时间筛选)
	@RequestMapping("/getlist")
	public Msg getListByShopclass(String shopclass, String starttime, String endtime) {// shopclass 0宾馆,1美食,2娱乐
		List<Shopuser> shopusers = service.getList(shopclass, starttime, endtime);
		return Msg.success().add("shopusers", shopusers);
	}

	// 查询服务伙伴详情
	@RequestMapping("/getShopuserByid")
	public Msg getShopuserByid(String shopid) {
		// 通过id查询服务伙伴详情
		Shopuser shopuser = service.getXQbyid(shopid);

		return Msg.success().add("shopuser", shopuser);
	}

	// 新建服务伙伴(添加操作日志)
	@RequestMapping("/addShopuser")
	public Msg addShopuser(@RequestBody Shopuser shopuser, String adminid) {
		boolean insResult = service.addShopuser(shopuser);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加一个服务伙伴");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 冻结服务伙伴(添加操作日志)-->1
	@RequestMapping("/frozenShopuser")
	public Msg frozenShopuser(String shopuserid, String adminid) {
		logger.info(shopuserid);
		boolean updResult = service.frozenShopuser(shopuserid);
		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("冻结了一个服务伙伴");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 解除冻结服务伙伴(添加操作日志)-->0
	@RequestMapping("/relieveFrozenShopuser")
	public Msg relieveFrozenShopuser(String shopuserid, String adminid) {
		logger.info(shopuserid);
		boolean updResult = service.relieveFrozenShopuser(shopuserid);
		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("解除冻结了一个服务伙伴");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 批量删除服务伙伴(添加操作日志)
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] suids, String adminid) {
		logger.info(suids.toString());
		logger.info("管理员ID" + adminid);

		boolean delResult = service.batchDelete(suids);

		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("批量删除了服务伙伴");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

//	// 查看操作日志
//	@RequestMapping("/getShopuserLog")
//	public Msg getShopuserLog() {
//		List<ShopuserLog> shopuserLogs = service.getShopuserLogs();
//		return Msg.success().add("shopuserLogs", shopuserLogs);
//	}

	// 批量导出服务伙伴(添加操作日志)
	@RequestMapping("batchExport")
	public void batchExport(String adminid, String shopclass, String starttime, String endtime,
			HttpServletResponse response, HttpServletRequest request) throws Exception {
		// 数据库查询数据 exportlist 需要导出的list
		List<Shopuser> shopusers = service.getList(shopclass, starttime, endtime);

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("服务伙伴列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("服务伙伴列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("用户id编号");
		headRow.createCell(1).setCellValue("注册时间");
		headRow.createCell(2).setCellValue("商家名称");
		headRow.createCell(3).setCellValue("账号");
		headRow.createCell(4).setCellValue("密码");
		headRow.createCell(5).setCellValue("冻结标识");
		headRow.createCell(6).setCellValue("名称");
		headRow.createCell(7).setCellValue("级别");
		headRow.createCell(8).setCellValue("价格");
		headRow.createCell(9).setCellValue("位置");
		headRow.createCell(10).setCellValue("备注");
		headRow.createCell(11).setCellValue("简介");
		headRow.createCell(12).setCellValue("类别： 0宾馆,1美食,2娱乐");

		// 遍历上面数据库查到的数据
		for (Shopuser su : shopusers) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(su.getId());
			dataRow.createCell(1).setCellValue(su.getZhucedate());
			dataRow.createCell(2).setCellValue(su.getShopname());
			dataRow.createCell(3).setCellValue(su.getUsername());
			dataRow.createCell(4).setCellValue(su.getPassword());
			dataRow.createCell(5).setCellValue(su.getFlag().equals("0") ? "正常" : "已冻结");
			dataRow.createCell(6).setCellValue(su.getSharename());
			dataRow.createCell(7).setCellValue(su.getSharelevel());
			dataRow.createCell(8).setCellValue(su.getShareprice());
			dataRow.createCell(9).setCellValue(su.getSharelocation());
			dataRow.createCell(10).setCellValue(su.getSharebeizhu());
			dataRow.createCell(11).setCellValue(su.getJianjie());
			dataRow.createCell(12).setCellValue(su.getShopclass());

		}
		// 下载导出文件名
		String filename = "服务伙伴列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

		// 添加操作日志
		Log log = new Log();
		log.setDate(DateUtils.getNowDate());
		log.setTime(DateUtils.getNowTime());
		log.setAdminid(adminid);
		log.setContent("批量导出服务伙伴");
		logservice.addLog(log);
	}

	// 修改商家信息
	@RequestMapping("/updateShopuser")
	public Msg updateShopuser(Shopuser shopuser, String adminid) {

		boolean updResult = service.updateShopuser(shopuser);
		if (updResult) {

			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改了服务伙伴信息");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加内容
	@RequestMapping("/addContext")
	public Msg addContext(@RequestBody ShopuserXQ shopuserxq) {
		// 添加详情返回id
		boolean insResult = service.addShopuserXQ(shopuserxq);
		if (insResult) {
			// 添加内容-图片
			List<ShopuserXQphoto> shopuserXQphotos = shopuserxq.getShopuserxqphoto();
			if (shopuserXQphotos != null && shopuserXQphotos.size() > 0) {
				for (ShopuserXQphoto shopuserXQphoto : shopuserXQphotos) {
					shopuserXQphoto.setShopuserxqid(String.valueOf(shopuserxq.getId()));
					service.addShopuserXQphoto(shopuserXQphoto);
				}
			}
			// 添加内容-预定说明
			List<ShopuserXQyudingsm> shopuserXQyudingsms = shopuserxq.getShopuserxqyudingsm();
			if (shopuserXQyudingsms != null && shopuserXQyudingsms.size() > 0) {
				for (ShopuserXQyudingsm shopuserXQyudingsm : shopuserXQyudingsms) {
					shopuserXQyudingsm.setShopuserxqid(String.valueOf(shopuserxq.getId()));
					service.addShopuserXQyudingsm(shopuserXQyudingsm);
				}
			}
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除内容
	@RequestMapping("/delContext")
	public Msg delContext(String shopuserxqid, String adminid) {
		boolean delResult = service.delContext(shopuserxqid);
		if (delResult) {

			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除了服务内容信息");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加预定说明
	@RequestMapping("/addYudingsm")
	public Msg addYudingsm(ShopuserXQyudingsm shopuserXQyudingsm, String adminid) {
		int insResult = service.addShopuserXQyudingsm(shopuserXQyudingsm);
		if (insResult > 0) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加了预定说明");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除预定说明
	@RequestMapping("/delYudingsm")
	public Msg delYudingsm(String yudingsmid, String adminid) {
		boolean delResult = service.delYudingsm(yudingsmid);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除了预定说明");
			logservice.addLog(log);

			return Msg.success();
		}
		return Msg.fail();
	}

	// 修改房间信息
	@RequestMapping("/updateShopuserXQ")
	public Msg updateShopuserXQ(@RequestBody ShopuserXQ shopuserxq) {
		boolean updResult = service.updateShopuserXQ(shopuserxq);
		if (updResult) {
			List<ShopuserXQphoto> shopuserXQphotos = shopuserxq.getShopuserxqphoto();
			if (shopuserXQphotos != null && shopuserXQphotos.size() > 0) {
				// 删除原图片
				service.deleteXQphotoByxqid(shopuserxq.getId());
				// 添加新图片
				for (ShopuserXQphoto shopuserXQphoto : shopuserXQphotos) {
					shopuserXQphoto.setShopuserxqid(String.valueOf(shopuserxq.getId()));
					service.insertXQphoto(shopuserXQphoto);
				}
			}
			return Msg.success();
		}
		return Msg.fail();
	}

}
